$(function () {
    $.ajax({
        url: '/lego/getJoinedTeamList',
        type: 'GET',
        async: false,
        success: function (data) {
            if (data && data.code == 200) {
                //用户加入过团队,默认团队下拉框显示用户已加入的团队
                $('#teamDiv').html(_.template($('#selectTeam_template').html())({
                    data: data.data
                }));
            }
        }
    });

    $.ajax({
        url: '/lego/getProjectListInCondition',
        type: 'GET',
        async: false,
        success: function (data) {
            if (data && data.code == 200) {
                $('#projectDiv').html(_.template($('#selectProject_template').html())({
                    data: data.data
                }));

                $('#projectId').change(function(){
                    var projectId = this.value;
                    if(projectId != -1){
                        //根据项目id查询对应的ip和port
                        $.ajax({
                            url: '/lego/getProjectDetail',
                            type: 'GET',
                            data: {"projectId":projectId},
                            success: function (data) {
                                if (data && data.code == 200) {
                                    $("#ipAndPort").html(data.data.ip + ":" + data.data.port);
                                }
                            }
                        });
                    }else {
                        $("#ipAndPort").html("");
                    }
                });
            }
        }
    });

    //*******************************请求头*************************************************
    $("#requestHeaderDiv").click(function(){
        var headerIndex = $("#headerIndex").val();//0-表示不显示请求头部输入框;1-表示显示请求头部输入框
        if(headerIndex == "0"){
            //请求头部输入框当前是不展示的,点击后需要展示头部输入框
            $("#headerIndex").val(1);
            $("#headerShowBtn").attr("class","glyphicon glyphicon-chevron-up").html("收缩");
            $("#inputHeaderDiv").css("display","block");
        }else if(headerIndex == "1"){
            //请求头部输入框当前是展示的,点击后需要不展示头部输入框
            $("#headerIndex").val(0);
            $("#headerShowBtn").attr("class","glyphicon glyphicon-chevron-down").html("展开");
            $("#inputHeaderDiv").css("display","none");
        }
    });

    $(".addHeaderBtn").click(function () {
        //获得表格的行数
        var rows = $("#inputHeaderTable").find("tr").length;
        var trHtml = "<tr>\n" +
            "         <td><input type=\"text\" class=\"form-control\" name=\"headerKeys\"></td>\n" +
            "         <td><input type=\"text\" class=\"form-control\" name=\"headerValues\"></td>\n" +
            "         <td>\n" +
            "             <button class=\"btn btn-default\" type=\"button\" onclick=\"deleteHeader(this)\">\n" +
            "                 <span class=\"glyphicon glyphicon-plus\" aria-hidden=\"true\"></span>delete\n" +
            "             </button>\n" +
            "         </td>\n" +
            "     </tr>";
        $("#inputHeaderTable tr").eq(rows-1).after(trHtml);
    });

    //**************************************************************************************

    //*******************************请求参数*************************************************

    $("#paramsKVHeader").click(function(){
        var paramsIndex = $("#paramsIndex").val();//0-表示不显示请求头部输入框;1-表示显示请求头部输入框
        if(paramsIndex == "0"){
            //请求头部输入框当前是不展示的,点击后需要展示头部输入框
            $("#paramsIndex").val(1);
            $("#paramsShowBtn").attr("class","glyphicon glyphicon-chevron-up").html("收缩");
            $("#paramsKVBodyDiv").css("display","block");
        }else if(paramsIndex == "1"){
            //请求头部输入框当前是展示的,点击后需要不展示头部输入框
            $("#paramsIndex").val(0);
            $("#paramsShowBtn").attr("class","glyphicon glyphicon-chevron-down").html("展开");
            $("#paramsKVBodyDiv").css("display","none");
        }
    });

    $(".addParamsBtn").click(function () {
        //获得表格的行数
        var rows = $("#inputParamsTable").find("tr").length;
        var trHtml = "<tr>\n" +
            "         <td><input type=\"text\" class=\"form-control\" name=\"paramKeys\"></td>\n" +
            "         <td><input type=\"text\" class=\"form-control\" name=\"paramValues\"></td>\n" +
            "         <td>\n" +
            "             <button class=\"btn btn-default\" type=\"button\" onclick=\"deleteHeader(this)\">\n" +
            "                 <span class=\"glyphicon glyphicon-plus\" aria-hidden=\"true\"></span>delete\n" +
            "             </button>\n" +
            "         </td>\n" +
            "     </tr>";
        $("#inputParamsTable tr").eq(rows-1).after(trHtml);
    });

    $("#requestType").change(function(){
        //根据不同的请求方法,显示不同的参数输入div
        if(this.value == "GET"){
            $("#paramsKVDiv").css("display","block");
            $("#paramsRawDiv").css("display","none");
        }else if(this.value == "POST"){
            $("#paramsKVDiv").css("display","none");
            $("#paramsRawDiv").css("display","block");
        }
    });
    //**************************************************************************************

    $("#addTestCaseBtn").click(function () {
        var url = $("#url").val();
        var teamId = $("#teamId").val();
        var projectId = $("#projectId").val();

        if(teamId == ""){
            window.wxc.xcConfirm("请选择团队!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        if(projectId == "-1"){
            window.wxc.xcConfirm("请选择项目!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        if(url == ""){
            window.wxc.xcConfirm("url不能为空!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        $.ajax({
            url: '/lego/addTestCase',
            type: 'POST',
            data:$("#testCaseForm").toMultiJSONString(),
            contentType:'application/json;charset=UTF-8',
            success: function (data) {
                if (data && data.code == 200) {
                    var op = {
                        title: "提示信息",
                        btn: window.wxc.xcConfirm.btnEnum.ok,
                    }
                    if(data.msg == "添加成功"){
                        window.wxc.xcConfirm(data.msg,window.wxc.xcConfirm.typeEnum.success, op);
                    }else {
                        window.wxc.xcConfirm(data.msg,window.wxc.xcConfirm.typeEnum.error, op);
                    }
                }
            }
        });
    });

    $(".executeTestCaseBtn").click(function () {
        //参数校验
        var url = $("#url").val();
        var teamId = $("#teamId").val();
        var projectId = $("#projectId").val();

        if(teamId == ""){
            window.wxc.xcConfirm("请选择团队!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        if(projectId == "-1"){
            window.wxc.xcConfirm("请选择项目!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        if(url == ""){
            window.wxc.xcConfirm("url不能为空!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }

        $.ajax({
            url: '/lego/validateBeforeExecute',
            type: 'POST',
            data:$("#testCaseForm").toMultiJSONString(),
            contentType:'application/json;charset=UTF-8',
            success: function (data) {
                if (data && data.code == 200) {
                    if(data.msg == "验证通过"){
                        executeTestCase();
                    }else {
                        window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
                    }
                }
            }
        });
    });

});

function executeTestCase() {
    $.ajax({
        url: '/lego/executeTestCase',
        type: 'POST',
        data:$("#testCaseForm").toMultiJSONString(),
        contentType:'application/json;charset=UTF-8',
        timeout:3000,
        success: function (data) {
            if (data && data.code == 200) {
                var responseStr = data.data["responseBody"];
                var isJsonStr = isJSON(responseStr);

                $(".executeResultTitle").css("display","block");
                $(".executeResult").css("display","block");

                if(isJsonStr){//如果是json格式数据,则格式化json
                    var options = {
                        dom: '.executeResult' //对应容器的css选择器
                    };
                    var jf = new JsonFormater(options); //创建对象
                    jf.doFormat(responseStr); //格式化json
                }else {
                    //不是json格式数据,直接展示
                    $(".executeResult").html(responseStr);
                }

                if($(".jsonPathCheckPoint").val() != ""){
                    var checkPointResultStr = data.data["checkPointResult"];
                    $(".executeResult").append(checkPointResultStr);
                }
            }
        },
        complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
            if(status=='timeout'){//超时,status还有success,error等值的情况
                $(".executeResult").css("display","block").html("请求超时!!!");
            }
        }
    });
}

function deleteHeader(obj) {
    $(obj).parents("tr").remove();
}
